package com.phiture.erp.produce.dal.mapper;

import java.math.BigDecimal;
import java.util.*;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.phiture.erp.produce.dal.entity.ErpProduceInItemsDO;
import org.apache.ibatis.annotations.Mapper;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;

/**
 * 生产入库明细 Mapper
 *
 * @author lekaihuai
 */
@Mapper
public interface ErpProduceInItemsMapper extends BaseMapperX<ErpProduceInItemsDO> {

    default List<ErpProduceInItemsDO> selectListByProduceInId(Long produceInId) {
        return selectList(ErpProduceInItemsDO::getProduceInId, produceInId);
    }

    default int deleteByProduceInId(Long produceInId) {
        return delete(ErpProduceInItemsDO::getProduceInId, produceInId);
    }

    default Map<Long, BigDecimal> selectProductionTaskCountSumMapByInIds(Collection<Long> inIds) {
        if (CollUtil.isEmpty(inIds)) {
            return Collections.emptyMap();
        }
        // SQL sum 查询
        List<Map<String, Object>> result = selectMaps(new QueryWrapper<ErpProduceInItemsDO>()
                    .select("source_bill_id, SUM(final_qty) AS sumCount")
                .eq("deleted", 0)
                .groupBy("source_bill_id")
                .in("produce_in_id", inIds));
        // 获得数量
        return convertMap(result, obj -> (Long) obj.get("source_bill_id"), obj -> (BigDecimal) obj.get("sumCount"));
    }

}
